var pmIndex = 0;
$(document).ready(
				function() {
					
					quadrantLocation = "PIC3F";
					populateQuadrant('#seat-map-view', true, 8,
							quadrantLocation + 'D', 1, 1, false, 0, true);
					populateQuadrant('#seat-map-view', false, 8,
							quadrantLocation + 'C', 1, 1, true, 14, false);
					populateInvertQuadrant('#seat-map-view', true, 7,
							quadrantLocation + 'A', 1, 1, true, 24, true);
					populateInvertQuadrant('#seat-map-view', false, 9,
							quadrantLocation + 'B', 1, 1, false, 23, false);

					appendClearFix('#seat-map-view');

					$(".employee-seat")
							.each(
									function() {
										var seatId = $(this).children(
												'.seat-code').last().val();
										var lastName = $(this).children(
												'.last-name').last().val();
										var firstName = $(this).children(
												'.first-name').last().val();
										var coordinate = $(this).children(
												'.coordinate').last().val();
										var projectCode = $(this).children(
												'.project-code').last().val();
										var localNo = $(this).children(
												'.local-no').last().val();
										var popoverContent = '<p>' + lastName
												+ ", " + firstName + '</p>';
										var projectClass = projectCode.replace(
												' ', '');

										if (projectCode != undefined
												&& projectCode != '') {
											popoverContent += '<p>Project: '
													+ projectCode + '</p>';
										}

										if (coordinate != '0-0') {
											$('#' + seatId)
													.append(
															'<p>' + coordinate
																	+ '</p>');
										}

										if (localNo != 0) {
											$('#' + seatId)
													.addClass('hasLocal');
											popoverContent += '<p style="font-size:0.9em;">local: '
													+ localNo + '</p>';
											$('#' + seatId).append(
													'<p>(loc.)' + localNo
															+ '</p>');
										}

										$('#' + seatId).append(
												'<p>' + lastName + ', '
														+ firstName + '</p>');

										$("#" + seatId).popover(
												{
													html : true,
													title : $(this).children(
															'.seat-code')
															.last().val(),
													content : popoverContent,
													placement : 'top',
													container : 'body',
													trigger : 'hover'
												});

										$('#' + seatId).addClass('allocated');
										$('#' + seatId).addClass(projectClass);

									});

					$(".pull-right").children('.seat').addClass('pull-right');
					$(".block").zoomTarget({
						closeclick : true
					});
					$('.quadrant').zoomTarget({
						closeclick : true
					});

					appendLandMarks();
					doHacks();
					
					$(document).click(function(){
						//$('.seat').removeClass('selected-seat');
						$('.seat').popover('hide');
					});
					
					$('.quadrant').click(function(){
						$('.seat').popover('hide');
						//$('.seat').removeClass('selected-seat');
					});
					
					$('#dd-quadrant').change(
							function(evt) {
								$('.seat').removeClass('selected-seat');
								$('.seat').popover('hide');
								if($(this).val().toLowerCase() != 'all'){
									var quadrantId = "#" + $("#dd-building").val() + $("#dd-floor").val() + $("#dd-quadrant").val();
									 $(quadrantId).zoomTo({targetsize:1, duration:600, root:$(quadrantId).parent().parent()});
									 evt.stopPropagation();
								}
								else
								{
									$('#seat-map-view').zoomTo({targetsize:0.1, duration:600, root:$('#seat-map-view')});
								}
								
							});
					
					$('#dd-floor').change(function(){
						$('.seat').popover('hide');
						if($(this).val() == '3F'){
								$('#under-construction').hide();
								$('#floor-container').show();
						}
						else if($(this).val() == '4F'){
							$('#under-construction').show();
							$('#floor-container').hide();
						}
						
					});
					addFloorPost();
});

function populateQuadrant(selector, isNextRow, blockCount, quadrantLocation,
		currentColumn, currentRow, isBlockInverted, pmIdStart,
		isPMCubeFromRight) {
	if (isNextRow) {
		$(selector).append(
				'<div class="row" style="margin-bottom:15px;"></div>');
	}

	$(selector).children('.row').last().append(
			'<div class="col-md-6" id="' + quadrantLocation + '"></div>');

	$("#" + quadrantLocation).addClass('quadrant');

	var lastQuadrant = $(selector).children('.row').last()
			.children('.col-md-6').last();
	lastQuadrant
			.append('<div class="col-md-12" style="margin-bottom:15px;"></div>');
	lastQuadrant.append('<div class="clearfix"></div>');
	var lastRowBlock = lastQuadrant.children('.col-md-12').last();
	insertBlocksItem(lastRowBlock, blockCount, false, quadrantLocation,
			currentColumn, currentRow, 6, isBlockInverted, pmIdStart,
			isPMCubeFromRight);

	lastQuadrant
			.append('<div class="col-md-12" style="margin-bottom:15px;"></div>');
	lastQuadrant.append('<div class="clearfix"></div>');
	lastRowBlock = lastQuadrant.children('.col-md-12').last();
	insertBlocksItem(lastRowBlock, blockCount, true, quadrantLocation,
			currentColumn, currentRow + 3, 8, isBlockInverted, pmIdStart,
			isPMCubeFromRight);

	// lastQuadrant.append('<div class="row"
	// style="margin-bottom:15px;"></div>');
	// lastQuadrant.append('<div class="clearfix"></div>');
	//
	// var lastQuadrantRow = lastQuadrant.children('.row').last();
	// lastQuadrantRow.append('<div class="col-md-12"></div>');
	// lastQuadrantRow.append('<div class="clearfix"></div>');
	//
	// var lastRowBlock = lastQuadrantRow.children('.col-md-12').last();
	//
	// insertBlocksItem(lastRowBlock, blockCount, false, quadrantLocation,
	// currentColumn, currentRow, 6, isBlockInverted,
	// pmIdStart,isPMCubeFromRight);
	//
	// lastQuadrant.append('<div class="row"
	// style="margin-bottom:15px;"></div>');
	// lastQuadrant.append('<div class="clearfix"></div>');
	//
	// lastQuadrantRow = lastQuadrant.children('.row').last();
	// lastQuadrantRow.append('<div class="col-md-12"></div>');
	// lastQuadrantRow.append('<div class="clearfix"></div>');
	//
	// lastRowBlock = lastQuadrantRow.children('.col-md-12').last();
	//
	// insertBlocksItem(lastRowBlock, blockCount, true, quadrantLocation,
	// currentColumn, currentRow + 3, 8, isBlockInverted, pmIdStart,
	// isPMCubeFromRight);
}

function populateInvertQuadrant(selector, isNextRow, blockCount,
		quadrantLocation, currentColumn, currentRow, isBlockInverted,
		pmIdStart, isPMCubeFromRight) {
	if (isNextRow) {
		$(selector).append(
				'<div class="row" style="margin-bottom:15px;"></div>');
	}

	$(selector).children('.row').last().append(
			'<div class="col-md-6"  id="' + quadrantLocation + '"></div>');

	$("#" + quadrantLocation).addClass('quadrant');
	var lastQuadrant = $(selector).children('.row').last()
			.children('.col-md-6').last();

	lastQuadrant
			.append('<div class="col-md-12" style="margin-bottom:15px;"></div>');
	lastQuadrant.append('<div class="clearfix"></div>');
	var lastRowBlock = lastQuadrant.children('.col-md-12').last();
	insertBlocksItem(lastRowBlock, blockCount, false, quadrantLocation,
			currentColumn, currentRow, 8, isBlockInverted, pmIdStart,
			isPMCubeFromRight);

	lastQuadrant
			.append('<div class="col-md-12" style="margin-bottom:15px;"></div>');
	lastQuadrant.append('<div class="clearfix"></div>');
	lastRowBlock = lastQuadrant.children('.col-md-12').last();
	insertBlocksItem(lastRowBlock, blockCount, true, quadrantLocation,
			currentColumn, currentRow + 4, 6, isBlockInverted, pmIdStart,
			isPMCubeFromRight);

	//	
	// lastQuadrant.append('<div class="row"
	// style="margin-bottom:15px;"></div>');
	// lastQuadrant.append('<div class="clearfix"></div>');
	//
	// var lastQuadrantRow = lastQuadrant.children('.row').last();
	// lastQuadrantRow.append('<div class="col-md-12"></div>');
	// lastQuadrantRow.append('<div class="clearfix"></div>');
	//
	// var lastRowBlock = lastQuadrantRow.children('.col-md-12').last();
	//
	// insertBlocksItem(lastRowBlock, blockCount, false, quadrantLocation,
	// currentColumn, currentRow, 8, isBlockInverted, pmIdStart,
	// isPMCubeFromRight);
	//
	// lastQuadrant.append('<div class="row"
	// style="margin-bottom:15px;"></div>');
	// lastQuadrant.append('<div class="clearfix"></div>');
	//
	// lastQuadrantRow = lastQuadrant.children('.row').last();
	// lastQuadrantRow.append('<div class="col-md-12"></div>');
	// lastQuadrantRow.append('<div class="clearfix"></div>');
	//
	// lastRowBlock = lastQuadrantRow.children('.col-md-12').last();
	//
	// insertBlocksItem(lastRowBlock, blockCount, true, quadrantLocation,
	// currentColumn, currentRow + 4, 6, isBlockInverted, pmIdStart,
	// isPMCubeFromRight);
}

function insertBlocksItem(lastRowBlock, blockCount, hasPMCube,
		quadrantLocation, currentColumn, startRow, seatCount, isInverted,
		pmIdStart, isPMCubeFromRight) {
	for ( var i = 0; i < blockCount; i++) {
		var seatPerRowCount = 0;
		var currentRow = startRow;
		var firstColumn = currentColumn;
		var secondColumn = currentColumn + 1;
		var idName;
		lastRowBlock.append('<div class="block"></div>');
		for ( var j = 0; j < seatCount; j++) {
			if (seatPerRowCount == 2) {
				seatPerRowCount = 0;
				currentRow++;
			}
			seatPerRowCount++;
			if (isEven(j)) {
				idName = quadrantLocation + firstColumn + '-' + currentRow;
			} else {
				idName = quadrantLocation + secondColumn + '-' + currentRow;
			}
			lastRowBlock.children('.block').last().append(
					'<div class="seat" id="' + idName + '"></div>');
			if (isInverted) {
				lastRowBlock.children('.block').last().addClass('pull-right');
			}
		}

		if (hasPMCube) {
			lastRowBlock.children('.block').last().append(
					'<div class="pm-cube" id="PM' + pmIdStart + '"></div>');
			if (isPMCubeFromRight) {
				pmIdStart++;
			} else {
				pmIdStart--;
			}
		}

		currentColumn = currentColumn + 2;
	}
	lastRowBlock.append('<div class="clearfix"></div>');
}

function appendLandMarks() {
	var middleLandMark = '<div class="row" style="margin-bottom:15px;">'
			+ '<div id="infra" class="col-md-1">'
			+ '</div>'
			+ '<div class="col-md-1">'
				+ '<div id="data-center" class="col-md-12">' 
					+ '<p>Data Center</p>'
				+ '</div>' 
			+ '</div>'
			+ '<div class="col-md-3">'
				+ '<div id="stair-area" class="col-md-12">' 
					+ '<p>EE Area, Stairs</p>'
				+ '</div>' 
			+ '</div>'
			+ '<div class="col-md-4">'
				+ '<div id="large-area" class="col-md-12">'
					+ '<div id="elevator" class="col-md-1">' 
						+ '<p>Elev</p>'
					+ '</div>'
					+ '<div id="comfort-room" class="col-md-5">' 
						+ '<p>Comfort Room</p>'
					+ '</div>'
					+ '<div id="ee-area" class="col-md-6">' 
						+ '<p>EE Area, Stairs</p>'
					+ '</div>' 
				+ '</div>'
			+ '</div>' 
			+ '<div class="col-md-1">'
				+ '<div id="breakout-area" class="col-md-12">' 
					+ '<p>Breakout Area</p>'
				+ '</div>'
			+ '</div>' 
			+ '<div class="col-md-2">'
				+ '<div id="training-rooms" class="col-md-12">' 
					+ '<p>Training Rooms</p>'
				+ '</div>'
			+ '</div>' 
		+ '</div>';
	$(middleLandMark).insertAfter($('#seat-map-view').children('.row').first());
}

function appendClearFix(selector) {
	$(selector).append('<div class="clearfix"></div>');
}

// Check if a number is even
function isEven(n) {
	return isNumber(n) && (n % 2 == 0);
}

// Check if Number is Odd
function isOdd(n) {
	return isNumber(n) && (n % 2 == 1);
}

// Check if variable is a number
function isNumber(n) {
	return n === parseFloat(n);
}

function doHacks() {
	$('#PIC3FA14-1').parent().appendTo('#infra');
	$('#PIC3FA14-1').parent().removeClass('block');
	$('#PIC3FA14-1').parent().addClass('special-block');
	$("#PIC3FA13-5").parent().remove();

	$("#PIC3FA").children('.col-md-12').each(function() {
		$(this).children('.block').addClass('block-a');
		$(this).children('.block-a').removeClass('block');
	});

	$('#PIC3FA').addClass('col-md-4');
	$('#PIC3FA').removeClass('col-md-6');
	$('<div id="reception-area" class="col-md-2"></div>').insertAfter($("#PIC3FA"));
	$('#reception-area').append('<div id="reception" class="col-md-12"><p>Reception</p></div><div id="breakout-area-6" class="col-md-12"><p>Breakout Area</p></div>');

	$('#PIC3FD').addClass('col-md-5');
	$('#PIC3FD').removeClass('col-md-6');
	$('#PIC3FD')
			.parent()
			.prepend(
					'<div class="col-md-1"><div id="big-pantry" class="col-md-12"><p>Pantry 1</p></div></div>');

	$('#PIC3FC').addClass('col-md-5');
	$('#PIC3FC').removeClass('col-md-6');

	$("#PIC3FD").children('.col-md-12').each(function() {
		$(this).children('.block').addClass('block-d');
		$(this).children('.block-d').removeClass('block');
	});

	$("#PIC3FC").children('.col-md-12').each(function() {
		$(this).children('.block').addClass('block-c');
		$(this).children('.block-c').removeClass('block');
	});

	$("#top-landmark").prependTo($("#seat-map-view"));
	$(
			'<div class="col-md-1"><div id="breakout-area-3" class="col-md-12"><p>Breakout Area</p></div></div>')
			.insertAfter('#PIC3FC');

	$("#bottom-landmark").appendTo($("#seat-map-view"));
	// $('<div class="col-md-1" style="padding-top:120px;"><div
	// id="breakout-area-4"
	// class="col-md-12"></div></div>').insertAfter('#PIC3FB');
	// $('#PIC3FA').append('<div id="speacial-quadrant"
	// class="col-md-10"></div>');
	// $('#PIC3FA').children('.row').each(function(){
	// $(this).children('.col-md-12').first().attr('style',
	// 'margin-bottom:15px;');
	// $(this).children('.col-md-12').first().appendTo('#speacial-quadrant');
	// });
	//	
	// $('#PIC3FA').children('.row').remove();
	// $('#PIC3FA').children('.clearfix').remove();
	//	
	// $('#speacial-quadrant').children('.col-md-12').each(function(){
	// $(this).children().removeClass('block');
	// $(this).children('.pull-right').addClass('block-a');
	// });
	//	
	// $('#PIC3FA').append('<div id="reception" class="col-md-2"></div>');
}

function addFloorPost(){
	$('#PIC3FD3-4').empty();
	$('#PIC3FD3-4').addClass('post');
	$('#PIC3FD3-4').append('<p>POST</p>');
	$('#PIC3FD3-4').popover('destroy');
	
	$('#PIC3FD7-4').empty();
	$('#PIC3FD7-4').addClass('post');
	$('#PIC3FD7-4').append('<p>POST</p>');
	$('#PIC3FD7-4').popover('destroy');
	
	$('#PIC3FD11-4').empty();
	$('#PIC3FD11-4').removeClass('allocated');
	$('#PIC3FD11-4').addClass('post');
	$('#PIC3FD11-4').append('<p>POST</p>');
	$('#PIC3FD11-4').popover('destroy');
	
	$('#PIC3FD12-4').empty();
	$('#PIC3FD12-4').removeClass('allocated');
	$('#PIC3FD12-4').addClass('post');
	$('#PIC3FD12-4').append('<p>POST</p>');
	$('#PIC3FD12-4').popover('destroy');
	
	$('#PIC3FD16-4').empty();
	$('#PIC3FD16-4').addClass('post');
	$('#PIC3FD16-4').append('<p>POST</p>');
	$('#PIC3FD16-4').popover('destroy');
	
	$('#PIC3FC13-4').empty();
	$('#PIC3FC13-4').addClass('post');
	$('#PIC3FC13-4').append('<p>POST</p>');
	$('#PIC3FC13-4').popover('destroy');
	
	$('#PIC3FC8-4').empty();
	$('#PIC3FC8-4').addClass('post');
	$('#PIC3FC8-4').append('<p>POST</p>');
	$('#PIC3FC8-4').popover('destroy');
	
	$('#PIC3FC4-4').empty();
	$('#PIC3FC4-4').removeClass('allocated');
	$('#PIC3FC4-4').addClass('post');
	$('#PIC3FC4-4').append('<p>POST</p>');
	$('#PIC3FC4-4').popover('destroy');
}